package common;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

interface operation {
    boolean check(int val);
}

public class Requirement {
    static String p = "((\\d+) (\\d+) (\\d+) (>=|>|=|<=|<|!=)\\s*(\\d+) match (.*))";
    static Pattern r =  Pattern.compile(p);

    public int packetSpace;

    operation o = (val)->true;

    int index;
    String op;
    int val;
    String pathExp;
    boolean ok = false;
    Device device;
    public Requirement(String line, Device d){
        device = d;
        readExp(line);
    }

    private void readExp(String line){
        Matcher m = r.matcher(line);
        if (m.find()){
            index = Integer.parseInt(m.group(2));
            long ipaddr = Long.parseLong(m.group(3));
            int prefixLength = Integer.parseInt(m.group(4));
            op = m.group(5);
            val = Integer.parseInt(m.group(6));
            pathExp = m.group(7);
            ok = true;
            packetSpace = device.bddEngine.encodeDstIPPrefix(ipaddr, prefixLength);
            genCheckFunc();
        }
    }

    public boolean check(int val){
        return o.check(val);
    }

    private void genCheckFunc(){
        switch (op){
            case ">=":{o = x -> x>=val;break;}
            case ">":{o = x -> x>val;break;}
            case "=":{o = x -> x==val;break;}
            case "<=":{o = x -> x<=val;break;}
            case "<":{o = x -> x<val;break;}
            case "!=":{o = x -> x!=val;break;}
            default:{ok=false;}
        }
    }

}
